import React from 'react';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Input, Button, Form, Icon, DatePicker } from 'antd';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { isEqual } from 'lodash';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { useRequest } from '@/hooks';
import { history } from 'umi'
import { Pagination } from '@/components';
import LeaveQuery from './LeaveQuery';
import LeaveTable from './LeaveTable';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function LeaveList({ data, loading, setCondition, condition, fetchList, sortOrders, form, type = '1', location }) {
    const { query } = location.location;
    /**
     * 获取表单数据
     */
    const getFormCondition = React.useCallback(async () => {
        let values = {};
        try {
            values = await form.validateFields();
        } catch (error) {
            console.log(error);
        }
        return values;
    }, []);
    // 表格变化
    const handleTableChange = async (pagination, filters, sorter) => {
        console.log('pagination, filters, sorter', pagination, filters, sorter);
        const values = await getFormCondition();
        // const con = Object.keys(filters).reduce(
        //     (ret, key) => {
        //         const item = filters[key];
        //         return {
        //             ...ret,
        //             [key]: item.length > 0 ? item[0] : ''
        //         };
        //     },
        //     { pageNo: 1 }
        // );
        const con = { pageNo: 1, ...filters };
        const con2 = {
            pageNo: 1,
            dimissionStatus: filters.dimissionStatus ? filters.dimissionStatus.join(',') : undefined
        };
        let orderBy = '';
        if (sorter && sorter.columnKey && sorter.order) {
            orderBy = `${sorter.columnKey} ${sorter.order.replace('end', '')}`;
        }
        // form.setFieldsValue({ ...condition, orderByField: orderBy, ...values, ...con });
        setCondition({ ...condition, sort: orderBy, ...values, ...con });
        history.push({ query: { ...condition, sort: orderBy, ...values, ...con2 } });
    };
    const reset = async () => {
        await form.resetFields();
        const values = await getFormCondition();
        setCondition({ ...condition, ...values });
        history.push({ query: { ...query, ...condition, ...values } });
    };
    // 点击查询
    const handleSearch = async val => {
        const values = await getFormCondition();
        const newPart = { ...values, ...val, pageNo: 1 };
        if (isEqual(condition, { ...query, ...condition, ...newPart })) {
            fetchList();
        } else {
            setCondition({ ...condition, ...newPart });
            history.push({
                query: {
                    ...query,
                    ...condition,
                    ...newPart,
                    dimissionStatus: values.dimissionStatus ? values.dimissionStatus.join(',') : undefined
                }
            });
        }
    };
    return (
        <div>
            <LeaveQuery form={form} handleSearch={handleSearch} reset={reset} type={type} location={location} />
            <LeaveTable
                dataSource={data?.data?.rows || []}
                handleTableChange={handleTableChange}
                loading={loading}
                sortOrders={sortOrders}
                type={type}
                fetchList={fetchList}
                location={location}
            />
            <Pagination
                current={condition?.pageNo}
                pageSize={condition?.pageSize}
                total={data?.data?.total}
                loading={loading}
                onChange={value => {
                    const { current, ...rest } = value;
                    setCondition({ ...condition, ...rest, pageNo: current });
                    history.push({
                        query: {
                            ...query,
                            ...condition,
                            ...rest,
                            pageNo: current,
                            dimissionStatus: query.dimissionStatus
                        }
                    });
                }}
            />
        </div>
    );
}
export default Form.create()(LeaveList);
